package com.xmy.cultivate.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.xmy.cultivate.entity.ServiceDetection;
import com.xmy.cultivate.entity.ServiceFeedback;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xmy.cultivate.entity.ServicePuchCard;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 * 服务反馈 Mapper 接口
 * </p>
 *
 * @author hpiggy
 * @since 2023-10-28
 */
public interface ServiceFeedbackMapper extends BaseMapper<ServiceFeedback> {

    @Select({"<script>",
            "SELECT* FROM (",
            "SELECT",
            "t01.id,",
            "t01.year_part,",
            "t01.quarter_num,",
            "t01.school_id,",
            "t01.student_id,",
            "t01.grade_id,",
            "t01.subjects_id,",
            "t01.lesson_num,",
            "t02.id as feedback_id,",
            "t02.sign_in02,",
            "t02.tag01,",
            "t02.tag02,",
            "t02.tag03,",
            "t02.tag04,",
            "t02.tag05,",
            "t02.tag06,",
            "t02.pass_num01,",
            "t02.show_pass_num01,",
            "t02.pass_num02,",
            "t02.show_pass_num02,",
            "t02.leave_msg01,",
            "t02.leave_msg02,",
            "t02.leave_msg03,",
            "t03.`name` as school_name,",
            "t04.`name` as teacher_name,",
            "t05.`teacher_id` as teacher_id,",
            "t05.`name` as grade_name,",
            "t05.year_class_id,",
            "t06.`name` as student_name,",
            "t07.`status` as  sign_in01,",
            "t07.`reach_type` as  reach_type,",
            "t07.is_repair,",
            "t07.lesson_type",
            "FROM",
            "( SELECT * FROM student_lesson_num",
            "WHERE deleted = 0",
            "<when test='yearPart!=null'>",
            "and `year_part` = ${yearPart}",
            "</when>",
            "<when test='quarterNum!=null'>",
            "AND quarter_num = ${quarterNum}",
            "</when>",
            "<when test='lessonNum!=null'>",
            "AND lesson_num = ${lessonNum}",
            "</when>",
            "<when test='subjectsId!=null'>",
            "AND subjects_id = ${subjectsId}",
            "</when>",
            "<when test='schoolId!=null'>",
            "AND school_id = ${schoolId}",
            "</when>",
            "<when test='gradeId!=null'>",
            "AND grade_id = ${gradeId}",
            "</when>",
            ") AS t01",
            "LEFT JOIN service_feedback AS t02 ON t01.id = t02.lesson_num_id",
            "LEFT JOIN organization AS t03 ON t01.school_id = t03.id",
            "LEFT JOIN grade AS t05 ON t05.id = t01.grade_id",
            "LEFT JOIN student AS t06 ON t06.id = t01.student_id",
            "LEFT JOIN staff AS t04 ON t04.id = t05.teacher_id",
            "LEFT JOIN record_course as t07 on t01.year_part = t07.year_part and t01.`quarter_num` = t07.`quarter` and t01.student_id = t07.student_id and t01.subjects_id = t07.subjects_id and t01.lesson_num = t07.lesson_num and t07.lesson_type =1",
            ") AS t where 1=1",
            "<when test='teacherId!=null'>",
            "and `teacher_id` = ${teacherId}",
            "</when>",
            "<when test='yearClassId!=null'>",
            "and `year_class_id` = ${yearClassId}",
            "</when>",
            "<when test='teacherId!=null and teacherId!=&apos;&apos;'>",
            "and id in (${idStrs})",
            "</when>",
            "</script>",
    })
    public IPage<ServiceFeedback> getFeedBadckList(IPage<?> page,@Param("yearPart") Integer yearPart,@Param("quarterNum") Integer quarterNum,@Param("schoolId") Long schoolId,@Param("yearClassId") Integer yearClassId,@Param("teacherId") Long teacherId,@Param("gradeId") Long gradeId,@Param("subjectsId") Long subjectsId,@Param("lessonNum") Integer lessonNum,@Param("studentName") String studentName,@Param("idStrs") String idStrs);

    @Select({"<script>",
            "SELECT* FROM (",
            "SELECT",
            "t01.id,",
            "t01.year_part,",
            "t01.quarter_num,",
            "t01.school_id,",
            "t01.student_id,",
            "t01.grade_id,",
            "t01.subjects_id,",
            "t01.lesson_num,",
            "t02.id as feedback_id,",
            "t02.sign_in02,",
            "t02.tag01,",
            "t02.tag02,",
            "t02.tag03,",
            "t02.tag04,",
            "t02.tag05,",
            "t02.tag06,",
            "t02.pass_num01,",
            "t02.show_pass_num01,",
            "t02.pass_num02,",
            "t02.show_pass_num02,",
            "t02.leave_msg01,",
            "t02.leave_msg_2,",
            "t02.leave_msg03,",
            "t03.`name` as school_name,",
            "t05.teacher_id,",
            "t04.`name` as teacher_name,",
            "t05.`teacher_id` as teacher_id,",
            "t05.`name` as grade_name,",
            "t05.year_class_id,",
            "t06.`name` as student_name,",
            "t07.`status` as  sign_in01,",
            "t07.`reach_type` as  reach_type,",
            "t07.is_repair",
            "FROM",
            "( SELECT * FROM student_lesson_num",
                "WHERE deleted = 0",
                    "and `id` = ${id}",
                ") AS t01",
                "LEFT JOIN service_feedback AS t02 ON t01.id = t02.lesson_num_id",
                "LEFT JOIN organization AS t03 ON t01.school_id = t03.id",
                "LEFT JOIN grade AS t05 ON t05.id = t01.grade_id",
                "LEFT JOIN student AS t06 ON t06.id = t01.student_id",
                "LEFT JOIN staff AS t04 ON t04.id = t05.teacher_id ",
                "LEFT JOIN record_course as t07 on t01.year_part = t07.year_part and t01.`quarter_num` = t07.`quarter` and t01.student_id = t07.student_id and t01.subjects_id = t07.subjects_id and t01.lesson_num = t07.lesson_num",
            ") AS t",
            "</script>",
    })
    public ServiceFeedback getFeedBackOne(@Param("id") Long id);

    @Select({"select * from service_detection where feedback_id = ${feedBackId}"})
    public List<ServiceDetection> getDetectiontListForFeedBackId(@Param("feedBackId") Long feedBackId);

    @Select({"select * from service_puch_card where feedback_id = ${feedBackId}"})
    public List<ServicePuchCard> getPuchCardListForFeedBackId(@Param("feedBackId") Long feedBackId);
}
